<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 朱亚杰 <zhuyajie@topthink.net>
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Admin;

use App\Models\AuthGroup;
use App\Models\AuthGroupAccess;
use App\Models\Menu;

/**
 * 权限管理控制器
 */
class AuthController extends BaseController
{

    public function __construct(AuthGroup $authGroup, Menu $menu,AuthGroupAccess $access)
    {
        parent::__construct();
        $this->authGroup = $authGroup;
        $this->menu = $menu;
        $this->access = $access;
    }

    /**
     * 权限管理首页
     */
    public function getIndex()
    {
        $rows = $this->authGroup->_getPageList();
        $rows['data'] = int_to_string($rows['data']);
        return view('admin.auth.index', compact('rows'));
    }

    /**
     * 创建管理员权限组
     */
    public function getAddGroup()
    {
        return view('admin.auth.edit_group');
    }

    /**
     * 编辑管理员权限组
     */
    public function getEditGroup()
    {
        $authGroup = $this->authGroup->_getOne(['id'=>request('id')]);
        return view('admin.auth.edit_group', compact('authGroup'));
    }

    /**
     * 管理员权限组数据写入/更新
     */
    public function postWriteGroup()
    {
        $res = $this->authGroup->addOrUpdate();
        if (!$res) {
            return $this->error($this->authGroup->getError());
        }
        return $this->success($res, urlAdmin('auth', 'getIndex'));
    }


    /**
     * 访问授权页面
     */
    public function getAccess()
    {

        $rules =  sortArrayAsc(levelTree($this->menu->getALLCache(), 'id', 'pid'));
        $authGroup = $this->authGroup->_getList(['status' => ['>',0]]);
        $thisGroup = [];
        $groupId = request('group_id');
        foreach ($authGroup as $v) {
            if($v['id'] == $groupId) {
                $thisGroup = $v->toArray();
            }
        }
        return view('admin.auth.manager_group', compact('authGroup','rules','thisGroup'));
    }


    /**
     * 状态修改
     */
    public function anyChangeStatus()
    {
        $method = request('method');
        $id = array_unique((array)request('id', 0));
        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($id)) {
            $id = [$id];
        }
        $where['id'] = ['in', $id];
        switch (strtolower($method)) {
            case 'forbid':
                return $this->forbid($this->authGroup, $where);
            case 'resume':
                return $this->resume($this->authGroup, $where);
            case 'delete':
                return $this->delete($this->authGroup, $where);
            default:
                return $this->error('参数非法');
        }
    }

    /**
     * 权限组授权管理员列表
     */
    public function getAdmin()
    {
        $groupId = request('group_id');
        if (empty($groupId)) {
            $this->error = '参数错误';
        }
        $authGroup = $this->authGroup->_getList(['status' => ['>',0]]);
        $thisGroup = [];
        foreach ($authGroup as $v) {
            if($v['id'] == $groupId) {
                $thisGroup = $v->toArray();
            }
        }
        $rows = $this->access->getAuthAccessAdminList($groupId);
        return view('admin.auth.admin', compact('authGroup','rules','thisGroup','rows'));
    }

    /**
     * 将管理员添加到权限组的编辑页面
     */
    public function getGroup()
    {
        $uid = I('uid');
        $auth_groups = D('AuthGroup')->getGroups();
        $user_groups = AuthGroupModel::getUserGroup($uid);
        $ids = array();
        foreach ($user_groups as $value) {
            $ids[] = $value['group_id'];
        }
        $nickname = D('Member')->getNickName($uid);
        $this->assign('nickname', $nickname);
        $this->assign('auth_groups', $auth_groups);
        $this->assign('user_groups', implode(',', $ids));
        $this->meta_title = '用户组授权';
        $this->display();
    }

    /**
     * 将管理员添加到权限组
     */
    public function postAddToGroup()
    {
        $res = $this->access->addToGroup();
        if (!$res) {
            return $this->error($this->access->getError());
        }
        return $this->success($res);
    }

    /**
     * 将管理员从权限组中移除
     */
    public function getRemoveFromGroup()
    {
        $res = $this->access->removeFromGroup();
        if (!$res) {
            return $this->error($this->access->getError());
        }
        return $this->success($res);
    }

}
